﻿using DA.AnimGraph;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace DA.AnimGraph.Editor {
    [AGBatchTool("Tool", "资源检查/批量重新生成资源引用计数")]
    [AGTool("Tool", "资源检查/重新生成资源引用计数")]
    public class Tool_CheckResources : IAGTool, IAGBatchTool {
        public void Execute(AGEContext ageContext) {
            Dictionary<AUID, int> count = new();
            var rootHandle = ageContext.GetRootHandle();
            var allGraphsUuid = rootHandle.GetAllGraphsUuid();
            for (int i = 0; i < allGraphsUuid.Length; i++) {
                var graphHandle = rootHandle.GetChildHandle(allGraphsUuid[i]) as AGAHandle_Graph;
                var allSubGraphsUuid = graphHandle.GetAllSubGraphsUuid();
                for (int j = 0; j < allSubGraphsUuid.Count; j++) {
                    var subHandle = graphHandle.GetChildHandle(allSubGraphsUuid[j]) as AGAHandle_SubGraph;
                    subHandle.ForeachNodes(nodeHandle => {
                        if (nodeHandle.data is IResourceNode rNode) {
                            var resArrayInNode = rNode.GetResources();
                            if (resArrayInNode == null) return;
                            foreach (var resInNode in resArrayInNode) {
                                if (count.TryGetValue(resInNode, out int refCount)) {
                                    count[resInNode] = refCount + 1;
                                } else {
                                    count.Add(resInNode, 1);
                                }
                            }
                            nodeHandle.SetDirty();
                        }
                    });
                }
            }

            if (count.Count != 0) {
                var resourceManager = ageContext.resourceManager;
                StringBuilder sb = new();
                sb.AppendLine($"资源引用计数更新 (资源总数： {count.Count})");
                int changeCount = 0;
                foreach (var c in count) {
                    var (isCorrect, oldRefCount) = resourceManager.CompareRefCount(c.Key, c.Value);
                    if (!isCorrect) {
                        sb.AppendLine($"{c.Key} , {oldRefCount} => {c.Value}");
                        changeCount++;
                    }
                }
                sb.AppendLine($"需更新资源总数： {changeCount}");
                Debug.Log(sb.ToString());
                ageContext.Save();
            }

        }
        public void Execute() {
            // 先保存当前打开的文件再开始
            var windows = AnimGraphEditorWindow.GetWindows();
            foreach (var window in windows) {
                window.SaveIfDirty();
            }
            var fileTool = AGEGlobal.GetInstance().fileTool;
            var fileList = fileTool.agaList;
            foreach (var file in fileList) {
                var context = new AGEContext(file.path, null);
                Execute(context);
            }
            foreach (var window in windows) {
                window.ReOpenWindow();
            }
        }
    }
}